//
//  CarFriendImg.m
//  LoveCar
//
//  Created by dlios on 15/5/30.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "CarFriendImg.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface CarFriendImg ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UIPageControl *p;

@end

@implementation CarFriendImg
- (void)dealloc
{
    [_getArray release];
    [_scroll release];
    [_p release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.getArray = [NSArray array];
    }
    return self;
}

-(void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self createScroll];
    [self createBack];
}
- (void)createBack
{
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 30, 30, 30)];
    [back setTitle:@"╳" forState:UIControlStateNormal];
    [back setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.view addSubview:back];
    [back addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [back release];
}
- (void)backAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)createScroll
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scroll.backgroundColor = [UIColor blackColor];
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width * self.getArray.count, 0); // 滚动范围
    //scroll.contentOffset = CGPointMake(100, 100); //偏移量
    //scroll.bounces = NO; //滚动到边缘能否继续滚动
    _scroll.pagingEnabled = YES; //滚动分页
    //scroll.scrollEnabled = NO; //滚动开关
    _scroll.showsHorizontalScrollIndicator = NO; //水平
    _scroll.showsVerticalScrollIndicator = NO; //垂直
    [self.view addSubview:_scroll];
    _scroll.contentOffset = CGPointMake(self.view.frame.size.width * self.page, 0);
    
    
    [_scroll release];
    
    for (int i = 0; i < self.getArray.count; i++) {
        
        UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [iView sd_setImageWithURL:[NSURL URLWithString:[self.getArray objectAtIndex:i]] placeholderImage:nil];
        NSRange range = [[self.getArray objectAtIndex:i] rangeOfString:@"w"];
        NSRange range1 = [[self.getArray objectAtIndex:i] rangeOfString:@"_"];
        NSInteger a = range1.location - range.location - 1;
        range.length = a;
        range.location = range.location + 1;
        NSString *str = [[self.getArray objectAtIndex:i] substringWithRange:range];
        
        range1.length = [[self.getArray objectAtIndex:i] length] - range1.location - 2;
        range1.location = range1.location + 2;
        NSString *str1 = [[self.getArray objectAtIndex:i] substringWithRange:range1];
        NSRange range3 = [str1 rangeOfString:@"_"];
        NSString *str2 = [str1 substringToIndex:range3.location];
        
        NSInteger width = [str integerValue];
        NSInteger height = [str2 integerValue];
        
        
        [iView setFrame:CGRectMake(self.scroll.frame.size.width * i, (self.view.frame.size.height - self.scroll.frame.size.width * height / width) / 2, self.scroll.frame.size.width, self.scroll.frame.size.width * height / width)];
        [self.scroll addSubview:iView];
        [iView release];
    }
    self.scroll.delegate = self;
    
    self.p = [[UIPageControl alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 150, 30, 300, 40)];
    _p.numberOfPages = self.getArray.count;
    _p.backgroundColor = [UIColor clearColor];
    [_p addTarget:self action:@selector(pAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_p];
    self.p.currentPage = self.page;
    [_p release];
}
- (void)pAction:(id)sender
{
    self.p = (UIPageControl *)sender;
    self.scroll.contentOffset = CGPointMake(self.p.currentPage * self.scroll.frame.size.width, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 计算滚动到哪一页
    NSInteger i = scrollView.contentOffset.x / self.view.frame.size.width;
    //    NSLog(@"page = %ld", i);
    //    CGFloat pageWidth=self.scroll.frame.size.width;
    //    int currentPage=floor((self.scroll.contentOffset.x-pageWidth/2)/pageWidth)+1;
    self.p.currentPage=i;
}


@end
